package com.yiibai.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.springmvc.controller.mycontroller;
import com.springmvc.entry.ContextConfig;

/**
 * Servlet implementation class Helloservlet
 */
//@WebServlet("/Helloservlet")
public class Entry extends HttpServlet {

	    private static final long serialVersionUID = 1L;

	    @Override
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	        //resp.setContentType("text/plain");
	        //resp.getWriter().write("Hello World! Maven Web Project Example.");
	    	this.doPost(req, resp);
	    }
	    @Override
	    public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			//System.out.println("123");
			ServletContext servletContext = this.getServletContext();
			//ContextConfig attribute = (ContextConfig)servletContext.getAttribute("contextConfig");
			//System.out.println(attribute.getAnnotationDrivenValue());
			//mycontroller object = (mycontroller)((HashMap<String, Object>)servletContext.getAttribute("beanList")).get("com.springmvc.controller.mycontroller");
			//object.dealMethod();
			HashMap<String, Object> beanList = (HashMap<String, Object>)servletContext.getAttribute("beanList");
			HashMap<String, Object> requestMap = (HashMap<String, Object>)servletContext.getAttribute("requestMap");
			ContextConfig contextConfig = (ContextConfig)servletContext.getAttribute("contextConfig");
			
			String requestURI = request.getRequestURI();
			int lastIndexOf = requestURI.lastIndexOf(".");
			String substring = requestURI.substring(0, lastIndexOf);
			HashMap<String,String> requestValueMap = (HashMap<String, String>)requestMap.get(substring);
			System.out.println(requestValueMap);
			if(requestValueMap!=null){
				Set<String> keySet = requestValueMap.keySet();
				Iterator<String> iterator = keySet.iterator();
				while(iterator.hasNext()){
					String next = iterator.next();
					if(next!=null&&next!=""){
						//System.out.println(next);
						String string = requestValueMap.get(next);
						Object object = beanList.get(next);
						Class<?> forName;
						try {
							forName = Class.forName(next);
							Method method = forName.getMethod(string, null);
							method.invoke(object, null);
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						
						
					}
				}
			}
			
		}
}
